package se480.tracker.proxy;

import org.apache.log4j.Logger;

import se480.tracker.event.TrackerEvent;
import se480.tracker.service.RegistrationService;
import se480.tracker.tracker.Tracker;
import se480.tracker.tracker.TrackerObserver;

/**
 * This is an synchronous proxy.  It abstracts the query client 
 * from the TrackerB oject.  Several clients could use a single
 * proxy.  This could help scaling.
 * 
 * Also; the proxy abstracts primary and backup Trackers from the clients, 
 * as well as the failover process.
 * 
 * @author mmichalak
 *
 */
public class SynchProxy implements TrackerProxy, TrackerObserver {
	static Logger logger = Logger.getLogger(SynchProxy.class.getName());
	
	RegistrationService regSvc = null;
	Tracker bTracker = null;

	public String processRequest(TrackerEvent ev) {
		logger.debug("Beginning process");
		
		if (bTracker == null) {
			bTracker = regSvc.getTrackerB(this);
		}
		
		String response = bTracker.processRequest(ev);
		return response;
	}

	public RegistrationService getRegSvc() {
		return regSvc;
	}

	public void setRegSvc(RegistrationService regSvc) {
		this.regSvc = regSvc;
	}

	public Tracker getaTracker() {
		return bTracker;
	}

	public void setaTracker(Tracker bTracker) {
		this.bTracker = bTracker;
	}

	/**
	 * Tracker Failover observer callback method
	 */
	public void updateTracker(Tracker tracker) {
		bTracker = tracker;
	}
}
